<?php
/*
 * 接口：/apply/add
 * 说明：新增申请
*/

//获取请求参数
$params = $route->bodyParams;

//查询用户所在节点的申请流程
$searchFlow = array(
  "ofa_offid" => $params['oap_process'],
  "ofa_root_osid" => $params['root_id'],
  "ofa_is_delete" => 0
);

$S1 = $db->mysqlDB->params($searchFlow)->select('easyd_oa_flow_approval');

if(count($S1['data'])<=0){
  $response->responseData(false,"该用户所属节点暂未创建审批流程，请联系管理人员进行确认");
}

//审核节点数组
$auditArr = $S1['data'][0]['ofa_son_osid'];

//审核流程ID
$ofaId = $S1['data'][0]['ofa_id'];

//获取审核流程经过节点数组
$sonNodeArr = explode("|",$auditArr);

//审核节点对应人员数组
$auditNodeAndUserArr = array();

//查询各个节点数组的审核人员
for($i=0;$i<count($sonNodeArr);$i++){

  $searchNodeUser = array(
    "ofam_osid" => $sonNodeArr[$i],
    "ofam_ofid" => $params['oap_process'],
    "ofam_ofaid" => $ofaId,
    "ofam_is_delete" => 0
  );

  $S2 = $db->mysqlDB->params($searchNodeUser)->select('easyd_oa_flow_approval_middle');

  $auditNodeAndUserArr["nodeId_".$sonNodeArr[$i]] = $S2['data'];

}

//关闭事物自动提交
$db->mysqlDB->falseAutoCommit();

//新增申请
$addApplyParams = array(
  "oap_uid" => $params['oap_uid'],
  "oap_process" => $params['oap_process'],
  "oap_content" => $params['oap_content'],
  "oap_create_time" => time()
);  

$applyId = $db->mysqlDB->automaticAdd($addApplyParams)->insert("easyd_oa_apply_process");

//生成审核任务流
//创建自己的任务流节点（为任务流的第一级）
$addMyProcessFlow = array(
  "opf_oapid" => $applyId,
  "opf_operator_id" => $params['oap_uid'],
  "opf_result" => 1,  //同意
  "opf_action" => 0,  //申请
  "opf_state"  => 2,  //处理完成
  "opf_crate_time" => time(),
  "opf_order_no" => 1
);

$R1 = $db->mysqlDB->automaticAdd($addMyProcessFlow)->insert("easyd_oa_process_flow");

//发送通知
$addNoticeParams = array(
  "on_receiver_id" => $params['oap_uid'],
  "on_content" => "您的申请已提交",
  "on_create_time" => time()
);
$addNoticeRS = $db->mysqlDB->automaticAdd($addNoticeParams)->insert("easyd_oa_notice");

//任务级别顺序
$processOrder = 1;

//新增是否有出错的记录
$isAddError = false;

foreach($auditNodeAndUserArr as $key=>$val){

  $processOrder++;

  //最后一级
  $isLast = 0;
  if($val == end($auditNodeAndUserArr)){
    $isLast = 1;
  }

  //第一级
  $processState = 0;
  if($val == reset($auditNodeAndUserArr)){
    $processState = 1;
  }

  for($k=0;$k<count($auditNodeAndUserArr[$key]);$k++){

    $nodeAndUserData = $auditNodeAndUserArr[$key][$k];

    $addOtherProcessFlow = array(
      "opf_oapid" => $applyId,
      "opf_operator_id" => $nodeAndUserData['ofam_uid'],
      "opf_action" => 1,  //审批
      "opf_state"  => $processState,
      "opf_crate_time" => time(),
      "opf_order_no" => $processOrder,
      "opf_is_last"  => $isLast
    );

    //创建任务流
    $R2 = $db->mysqlDB->automaticAdd($addOtherProcessFlow)->insert("easyd_oa_process_flow");

    if($processState == 1){

      //发送通知
      $addNoticeParams = array(
        "on_receiver_id" => $nodeAndUserData['ofam_uid'],
        "on_content" => "您有一个新的申请等待审批，请注意查看",
        "on_create_time" => time()
      );
      $addNoticeRS = $db->mysqlDB->automaticAdd($addNoticeParams)->insert("easyd_oa_notice");

    }

    //如果有出错则进行记录
    if(!$R2){
      $isAddError = true;
      break;
    }
  }

}

//判断是否有出错
if(!$applyId || $isAddError || !$R1){
  $db->mysqlDB->rollBackCommint();
  $db->mysqlDB->trueAutoCommit();
  $response->responseData(false);
}else{
  $db->mysqlDB->actionCommint();
  $db->mysqlDB->trueAutoCommit();
  $response->responseData(true);
}
?>